﻿using StockMatch.Service.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Titan;

namespace StockMatch.Service
{
    public class ProjectPrincipalService : ContextBoundObject
    {

        #region 增删改

        public bool Insert(ProjectPrincipal projectPrincipal)
        {
            try
            {
                using (var cn = Environment.OpenSession())
                {
                    return cn.Insert(projectPrincipal) > 0;
                }
            }
            catch (Exception ex)
            {
                throw new StockException(ex.Message);
            }
        }

        public bool Update(ProjectPrincipals projectPrincipals, params object[] updateProperty)
        {
            try
            {
                using (var cn = Environment.OpenSession())
                {
                    return cn.Update(projectPrincipals, updateProperty) > 0;
                    //向审批表添加条记录
                }
            }
            catch (Exception ex)
            {
                throw new StockException(ex);
            }
        }

        public bool Delete(int projectPrincipalId)
        {
            try
            {
                using (var cn = Environment.OpenSession())
                {
                    ProjectPrincipal projectPrincipal = new ProjectPrincipal() { ProjectPrincipalId = projectPrincipalId };
                    return cn.Delete(projectPrincipal) > 0;//物理删除还是伪删除?

                    //删除审批表中的记录
                }
            }
            catch (Exception ex)
            {
                throw new StockException(ex.Message);
            }
        }

        #endregion


        #region 查询

        public ProjectPrincipal GetProjectPrincipal(int projectPrincipalId)
        {
            try
            {
                using (var cn = Environment.OpenSession())
                {
                    ProjectPrincipal projectPrincipal = new ProjectPrincipal{ ProjectPrincipalId = projectPrincipalId };
                    if (cn.Select(projectPrincipal))
                    {
                        return projectPrincipal;
                    }
                    return null;
                }
            }
            catch (Exception ex)
            {
                throw new StockException(ex.Message);
            }
        }

        public ProjectPrincipals GetProjectPrincipals(QueryExpression query)
        {
            try
            {
                using (var cn = Environment.OpenSession())
                {
                    ProjectPrincipals projectPrincipals = new ProjectPrincipals();
                    projectPrincipals.TotalCount = cn.SelectCollection(projectPrincipals.Items, query);
                    return projectPrincipals;
                }
            }
            catch (Exception ex)
            {
                throw new StockException(ex.Message);
            }
        }

        #endregion
    }
}
